package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    /**
     * 根据月份获取会员数
     * @param months
     * @return
     */
    @Override
    public List<Integer> getMemberAndMonths(List<String> months) {
        // 创建集合用来存储count
        List<Integer> memberCount = new ArrayList<>();
        // 遍历取出月份,并查询当前月份之前的会员数量,放入list集合
        for (String month : months) {
            // 加上天
            String endDate = month+".31";
            // 调用方法获取count
            Long count = memberDao.selectCountByMonth(endDate);
            memberCount.add(count.intValue());
        }
        return memberCount;
    }

    @Override
    public Map<String, Object> getBusinessReportData() {
         // 根据前端分析 使用map集合存储
        Map<String, Object> reportData = new HashMap<>();

        // 将当前的日期转换成String
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");
        String strDate = sdf.format(date);

        try {
            //获得本周一日期
            String firstWeek = DateUtils.parseDate2String( DateUtils.getThisWeekMonday());
            //获得本月第一日期
            String firstMoon = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //会员数据统计
                  // 新增会员统计
                  Long integer = memberDao.queryMember(strDate);
                  // 查询总共会员数
                   Long totalSum = memberDao.queryMemberSum();
                  // 本周新增会员数
                  Long weekSum = memberDao.queryMemberWeek(firstWeek);
                  // 本月新增会员数
                  Long MoonSum = memberDao.queryMemberMoon(firstMoon);
             //预约到诊数据统计
                    // 今日预约人数
                   Long todaySum = orderDao.queryDay(strDate);
                    // 今日到诊人数
                    Long dayNumber = orderDao.queryDayGo(strDate);
                    // 本周预约人数
                    Long OrderNumber = orderDao.queryOrderNumber(firstWeek);
                    // 本周到诊人数
                    Long VisitsNumber = orderDao.queryVisitsNumber(firstWeek);
                    // 本月预约人数
                    Long MonthOrderNumber = orderDao.queryMonthOrderNumber(firstMoon);
                     // 本月到诊人数
                     Long MonthVisitsNumber = orderDao.queryMonthVisitsNumber(firstMoon);
                       log.info("[本月到诊人数]"+todaySum);
             // 套餐存储
             List<Map> maps = orderDao.queryhotSetmeal();

             // 将获取到所有数据全部存储到map集合

            reportData.put("reportDate",strDate);
            reportData.put("todayNewMember",integer);
            reportData.put("totalMember",totalSum);
            reportData.put("thisWeekNewMember",weekSum);
            reportData.put("thisMonthNewMember",MoonSum);
            reportData.put("todayOrderNumber",todaySum);
            reportData.put("todayVisitsNumber",dayNumber);
            reportData.put("thisWeekOrderNumber",OrderNumber);
            reportData.put("thisWeekVisitsNumber",VisitsNumber);
            reportData.put("thisMonthOrderNumber",MonthOrderNumber);
            reportData.put("thisMonthVisitsNumber",MonthVisitsNumber);
            reportData.put("hotSetmeal",maps);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

        }



        return reportData;
    }


}
